import Koa from 'koa';
import appConfig from './config/app';
import bootstrap from './middlewares/bootstrap';
import response from './middlewares/response';
import router from './routes';
import { accessLogger } from './utils/logger';
import body from 'koa-body';
import authorization from './middlewares/authorization';
import operationLog from './middlewares/operationLog';
import staticServe from 'koa-static';
import path from 'path';

const debug = require('debug')('koa-weapp-demo');

const app = new Koa();

// 接口文档
app.use(
    staticServe(path.join(path.dirname(__dirname), 'static'), {
        index: 'index.html',
        hidden: false,
        defer: false,
    }),
);

// 启动引导
app.use(bootstrap);

// 用户操作日志
app.use(operationLog);

// 访问日志
app.use(accessLogger());

// 使用响应处理中间件
app.use(response);

// 解析请求体
app.use(body());

// 用户认证
app.use(authorization);

// 引入路由分发
app.use(router.routes());

// 启动程序，监听端口
app.listen(appConfig.port, () => debug(`listening on port ${appConfig.port}`));
